<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      @close="resetForm"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="站点" prop="site_id">
              <el-select v-model="ruleForm.site_id" style="width: 100%" size="mini" clearable filterable placeholder="请选择站点">
                <el-option
                  v-for="item in optionsSite"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备序列号" prop="device_num">
              <el-input v-model="ruleForm.device_num" size="mini" clearable placeholder="请输入设备序列号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="device_name">
              <el-input v-model="ruleForm.device_name" size="mini" clearable placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型">
              <el-select v-model="ruleForm.device_type" style="width: 100%" size="mini" clearable filterable placeholder="请选择设备类型">
                <el-option
                  v-for="item in optionsType"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装时间">
              <el-date-picker
                v-model="ruleForm.install_time"
                style="width: 100%"
                type="datetime"
                clearable
                size="mini"
                placeholder="选择安装时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装地址">
              <el-input v-model="ruleForm.address" size="mini" clearable placeholder="请输入安装地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP">
              <el-input v-model="ruleForm.ip" size="mini" clearable placeholder="请输入IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备端口号">
              <el-input v-model="ruleForm.device_port" size="mini" clearable placeholder="请输入设备端口号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备用户名">
              <el-input v-model="ruleForm.device_user" size="mini" clearable placeholder="请输入设备用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备密码">
              <el-input v-model="ruleForm.device_pass" type="password" size="mini" clearable placeholder="请输入设备密码" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="直播源地址1">
              <el-input v-model="ruleForm.address1" size="mini" clearable placeholder="请输入直播源地址1" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="直播源地址2">
              <el-input v-model="ruleForm.address2" size="mini" clearable placeholder="请输入直播源地址2" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" @click="resetForm" size='mini'>取消</el-button>
          <el-button type="primary" @click="addOrEdit" size='mini'>确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { createVideoFile, updateVideoFile, detailVideoFile, stationAllList, getDictionary } from '@/api/fileManage'
export default {
  name: 'detail',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      ruleForm: {
        site_id: '', // 站点
        device_num: '', // 设备序列号
        device_name: '', // 设备名称
        install_time: '', // 安装时间
        device_type: '', // 设备类型
        address: '', // 安装地址
        address1: '', // 直播源地址1
        address2: '', // 直播源地址2
        ip: '', // ip
        device_port: '', // 设备端口号
        device_user: '', // 设备用户名
        device_pass: '' // 设备密码
      },
      rules: {
        site_id: [
          { required: true, message: '请选择站点', trigger: 'blur' }
        ],
        device_num: [
          { required: true, message: '请输入设备序列号', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '请输入设备名称', trigger: 'blur' }
        ]
      },
      optionsType: [],
      optionsSite: []
    }
  },
  created() {
    this.getOptions()
    // 编辑
    if (this.id) {
      // this.ruleForm.pid = this.id
      if (this.title === '新增') {
        this.ruleForm.pid = this.id
      } else {
        this.getDetailById(this.id)
      }
    }
  },
  methods: {
    // 获取所有站点
    getOptions() {
      stationAllList().then(res => {
        // console.log('站点名称', res)
        this.optionsSite = res.data.data
      })
      getDictionary({
        pid: 8
      }).then(res => {
        this.optionsType = res.data.dictionaries
      })
    },
    // 获取详情
    getDetailById(id) {
      detailVideoFile({ id }).then(res => {
        this.ruleForm = res.data
        this.ruleForm.device_type += ''
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.title === '编辑') {
            //  是编辑
            updateVideoFile(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            createVideoFile(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    },
    handleChange(e) {
      // console.log(e)
    }
  }
}
</script>

<style scoped>

</style>
